package bsts.view.user;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import bsts.application.BSTSApplication;
import bsts.controller.UserController;
import bsts.model.User;
import bsts.exception.InvalidAccessException;

import bsts.util.validation.LengthValidation8;

public class LoginPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private BSTSApplication manager;
	private JPanel pnlMain;
	private JPanel pnlCredential;
	private JPanel pnlButtons;
	private JPanel pnlMessage;
	private JLabel lblUsername;
	private JLabel lblPassword;
	private JLabel lblMessage;
	private JTextField txtUsername;
	private JPasswordField txtPassword;
	private JButton btnLogin;
	private JButton btnExit;

	private ActionListener actlis_btnLogin;
	private ActionListener actlis_btnExit;

	private UserController userController;
	
	private LengthValidation8 lv8 = new LengthValidation8();

	public LoginPanel(BSTSApplication manager) {
		this.manager = manager;

		pnlMain = new JPanel();
		pnlCredential = new JPanel();
		pnlButtons = new JPanel();
		pnlMessage = new JPanel();

		lblUsername = new JLabel("Username:");
		lblPassword = new JLabel("Password:");
		lblMessage = new JLabel();
		lblMessage.setPreferredSize(new Dimension(340, 16));
		txtUsername = new JTextField();
		txtPassword = new JPasswordField();
		btnLogin = new JButton("Login");
		btnExit = new JButton("Exit");
		
		pnlMain.setLayout(new BoxLayout(pnlMain, BoxLayout.Y_AXIS));
		pnlCredential.setLayout(new GridLayout(2, 2));
		pnlButtons.setLayout(new FlowLayout(FlowLayout.CENTER));
		pnlMessage.setLayout(new FlowLayout(FlowLayout.CENTER));

		pnlCredential.add(lblUsername);
		pnlCredential.add(txtUsername);
		pnlCredential.add(lblPassword);
		pnlCredential.add(txtPassword);

		pnlButtons.add(btnLogin);
		pnlButtons.add(btnExit);

		pnlMessage.add(lblMessage);

		pnlMain.add(pnlCredential);
		pnlMain.add(pnlButtons);
		pnlMain.add(pnlMessage);

		this.add(pnlMain);

		initActionListener();

		btnLogin.addActionListener(actlis_btnLogin);
		btnExit.addActionListener(actlis_btnExit);
		
		//Testing
		txtUsername.setInputVerifier(lv8);
		txtUsername.addActionListener(lv8);
		//Testing

		userController = new UserController();
	}

	private void initActionListener() {
		actlis_btnLogin = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				userController = new UserController();

				try {
					User user = userController.verifyUser(
							txtUsername.getText(),
							new String(txtPassword.getPassword()));

					manager.loginSuccess(user);
				} catch (InvalidAccessException iae) {
					lblMessage
							.setText("The username or password is incorrect. Please try again.");
				}
			}
		};

		actlis_btnExit = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.shutdown();
			}
		};
	}

}
